#include "MainScreen.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "PhiysicsSprite.h"
#include "PhysicsLayer.h"


using namespace cocos2d;
using namespace CocosDenshion;

bool CMainScreen::init()
{
	if ( !CCLayer::init() )
	{
		return false;
	}
	
	m_nZOrder = 0;
	
	SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("background.wav")).c_str());
	SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("background.wav")).c_str(),true);
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCSprite* pSprite = CCSprite::create("MainScreenWall.png");
	pSprite->setPosition( ccp(size.width/2, size.height/2) );
	this->addChild(pSprite, ++m_nZOrder);

	CCLabelTTF* pLabel = CCLabelTTF::create("Here's gonna be a menu list", "Thonburi", 34);
	pLabel->setPosition( ccp(size.width / 2, size.height - 20) );
	this->addChild(pLabel, ++m_nZOrder);

	cocos2d::CCMenuItemImage* pGotoHWScreen = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(CMainScreen::GoToHWSPressed));
	pGotoHWScreen->setPosition(ccp(size.width/2 - 150, size.height/2));

	CCLabelTTF* pMenuLabel = CCLabelTTF::create("Go to HelloWorld Screen", "Thonburi", 34);
	pMenuLabel->setPosition( ccp(size.width/2 + 50, size.height/2) );
	this->addChild(pMenuLabel,++m_nZOrder);

	CCMenuItemImage *pMoveKolobokToLeft = CCMenuItemImage::create("arrow_left.png", "arrow_left.png", this, menu_selector(CMainScreen::MoveToLeft));
	CCMenuItemImage *pMoveKolobokToRight = CCMenuItemImage::create("arrow_right.png", "arrow_right.png", this, menu_selector(CMainScreen::MoveToRight));
	
	pMoveKolobokToLeft->setPosition(CCPointMake(size.width - pMoveKolobokToLeft->getContentSize().width*1.5, pMoveKolobokToLeft->getContentSize().height/2));
    pMoveKolobokToRight->setPosition(CCPointMake(size.width - pMoveKolobokToLeft->getContentSize().width*0.5, pMoveKolobokToLeft->getContentSize().height/2));

	CCMenu *menu = CCMenu::create(pMoveKolobokToLeft, pMoveKolobokToRight, NULL);
    menu->setPosition(CCPointZero);

	addChild(menu, 15);

	CCMenu* pMenu = CCMenu::create(pGotoHWScreen, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, ++m_nZOrder);

	pPhysicsLayer = new CPhysicsLayer();
	this->addChild(pPhysicsLayer,++m_nZOrder,1);
	m_pCloudNode = pPhysicsLayer->InitSprite("Cloud.png");
	m_pColobokNode = pPhysicsLayer->InitSprite("ball.png");
	pPhysicsLayer->addBatchNodeAtPosition(m_pColobokNode,ccp(size.width/2 + 50, size.height/2));
	
	AddClouds();

	return true;
}

cocos2d::CCScene* CMainScreen::scene()
{
	CCScene *pScene = CCScene::create();
	CMainScreen* pLayer = CMainScreen::create();
	pScene->addChild(pLayer);

	return pScene;
}

void CMainScreen::GoToHWSPressed(CCObject* pSender)
{
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::create(2, HelloWorld::scene()));
}

void CMainScreen::AddClouds(int NumOfInstances)
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	
	for (size_t i = 0; i<NumOfInstances; i++)
	{
		CCSprite* pCloud = CCSprite::create("Cloud.png");
		pCloud->setPosition(ccp(size.width, size.height/2+150));
		pCloud->setScale(0.3);
		this->addChild(pCloud, m_nZOrder);

		// Determine speed of the target
		int minDuration = (int)1;
		int maxDuration = (int)4;
		int rangeDuration = maxDuration - minDuration;
		// srand( TimGetTicks() );
		int actualDuration = ( rand() % rangeDuration ) + minDuration;


		// Create the actions
		CCFiniteTimeAction* actionMove = CCMoveTo::actionWithDuration( (float)actualDuration/0.5, ccp(0-size.width, size.height/2+150) );
		CCFiniteTimeAction* actionMoveDone = CCCallFuncN::actionWithTarget( this, callfuncN_selector(CMainScreen::CloudMoveFinished));
		
		pCloud->runAction( CCSequence::actions(actionMove, actionMoveDone, NULL) );
	}
}

void CMainScreen::CloudMoveFinished(CCNode* sender)
{
	CCSprite *pCloud = (CCSprite *)sender;
	this->removeChild(pCloud,true);
	AddClouds(1);
}

void CMainScreen::MoveToLeft(CCObject* pSender)
{
	pPhysicsLayer->MoveBatchNodeToLeft(m_pColobokNode);
}

void CMainScreen::MoveToRight(CCObject* pSender)
{
	pPhysicsLayer->MoveBatchNodeToRight(m_pColobokNode);
}

void CMainScreen::initCar()
{
	
}
